package com.e3.basdoc.measure.service.impl;

import java.util.List;

import com.e3.basdoc.measure.dao.MeasureUnitDAO;
import com.e3.basdoc.measure.entity.MeasureUnitEntity;
import com.e3.basdoc.measure.model.MeasureUnitModel;
import com.e3.basdoc.measure.service.MeasureUnitCacheManager;
import com.e3.basdoc.measure.service.MeasureUnitService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperService;

public class MeasureUnitServiceImpl extends SuperService implements	MeasureUnitService {

	private MeasureUnitDAO measureDAO;
	
	private MeasureUnitDAO getMeasureUnitDAO(){
		if(measureDAO == null){
			measureDAO = DAOHelper.createDAOInstance(MeasureUnitDAO.class);
		}
		return measureDAO;
	}

	public MeasureUnitModel newMeasureUnit(MeasureUnitModel model) throws ServiceException {
		validateMeasureUnit(model);
		checkMeasureUnitExisted(model);
		
		MeasureUnitEntity entity = measureModelToEntity(model, null);
		getMeasureUnitDAO().persist(entity);
		
		model.setId(entity.getId());
		
		MeasureUnitCacheManager.INSTANCE.put(model);
		
		return model;
	}

	public MeasureUnitModel modifyMeasureUnit(MeasureUnitModel model) throws ServiceException {
		validateMeasureUnit(model);
		
//		String strId = model.getId();
//		long id = Long.parseLong(strId == null ? "0" : strId);
		long id = model.getId();

		MeasureUnitEntity entity = getMeasureUnitDAO().getById(MeasureUnitEntity.class, id);
		if (entity == null) {
			return newMeasureUnit(model);
		}

		String oldCode = entity.getCode();
		if (!oldCode.equals(model.getCode())) {
			checkMeasureUnitExisted(model);
		}

		measureModelToEntity(model, entity);
		getMeasureUnitDAO().merge(entity);
		
		MeasureUnitCacheManager.INSTANCE.put(model);
		return model;
	}

	public void deleteMeasureUnitByIds(List<Long> ids) throws ServiceException {
		if(ids == null||ids.isEmpty()){
			return;
		}
		
		getMeasureUnitDAO().deleteMeasureUnitByIds(ids);
		
		MeasureUnitCacheManager.INSTANCE.remove(ids);
	}

	public void checkMeasureUnitExisted(MeasureUnitModel model) throws ServiceException {
		if (model == null) {
			return;
		}

		String code = model.getCode();
		String name = model.getName();
		
		StringBuffer msgs = new StringBuffer();
		long size = getMeasureUnitDAO().getMeasureUnitSizeByCode(code);
		if(size>0){
			msgs.append("编码为'" + code+"' ");
		}
		size = getMeasureUnitDAO().getMeasureUnitSizeByName(name);
		if (size>0) {
			msgs.append("名称为'" + name + "' ");
		}
		if(msgs.length()>0){
			msgs.append("已经存在，请设置新的编码和名称。");
			throw new ServiceException(msgs.toString());
		}
		
	}

	private void validateMeasureUnit(MeasureUnitModel model) throws ServiceException{
		String code = model.getCode();
		String name = model.getName();

		if (code == null || code.trim().length() == 0 
			|| name == null || name.trim().length() == 0
			) {
			throw new ServiceException("请设置编码和名称。");
		}
	}
	
	private MeasureUnitEntity measureModelToEntity(MeasureUnitModel model,MeasureUnitEntity entity){
		entity = modelToEntity(MeasureUnitEntity.class, model, entity);

		entity.setCode(model.getCode());
		entity.setName(model.getName());
		
		return entity;

	}
}
